<?php
	Class Connect{
		
		private $server = 'localhost';
		private $user = 'root';
		private $pass = 'root';
		private $db = 'escup';
		private $lastid;
		
		public function getServer() {
		return $this->server;
		}

		public function getUser() {
		return $this->user;
		}

		public function getPass() {
		return $this->pass;
		}

		public function getDb() {
		return $this->db;
		}

		public function getLastid() {
		return $this->lastid;
	}

		public function setServer($server) {
		$this->server = $server;
		}

		public function setUser($user) {
		$this->user = $user;
		}

		public function setPass($pass) {
		$this->pass = $pass;
		}

		public function setDb($db) {
		$this->db = $db;
		}
		
		public function Connect(){
			$this->server;
			$this->user ;
			$this->pass;
			$this->db ;
		}

		/* connection to the mysql databse */
		public function db_connect(){
			$connect = mysql_connect($this->server,$this->user,$this->pass);
			if(!$connect){
				$this->my_error("Unable connect to databse");
			}
			$selectdb = mysql_select_db($this->db,$connect);
			if(!$selectdb){
				$this->my_error("Unable to select databse");
			}
			return $connect;
		}
		
		/* disconnection from the mysql databse */
		public function db_disconnect(){
				mysql_close();
		}
		
		/* Display errors */
		public function my_error($err){
				print("error : $err<br>\n");
				print("Aborting<br>\n");
				print("SQL reports :");
				print(mysql_error());
				print("<br>\n");
				exit();			
		}
	
		public function query($sql){
			if($this->db_connect()){
				$array = array();			
				$query = mysql_query($sql);
				if(!$query){
					echo ($sql);
					$this->my_error("Could not run your sql query");
				}
				if(eregi("^SELECT",$sql)){
					$mysql_num_row = mysql_num_rows($query);
					if($mysql_num_row == 0){
						$array = false;
					}
					elseif ($mysql_num_row > 0){
						while($row = mysql_fetch_assoc($query)){
							$array[] = $row ;
						}
					}
					mysql_free_result($query);
				}
				if(eregi("^INSERT",$sql)){
					$this->lastid = mysql_insert_id($sql);
				}
				$this->db_disconnect();
				if(eregi("^SELECT",$sql)){
					return $array;
				}
				return true;
			}
			return false;
		}
		
		public function findAll($table,$condition_array = null){
			if ($condition_array != null){
				$i = 0;
				$count = count($condition_array);
				$conditions = "WHERE ";
				foreach ($condition_array as $key => $value){
					$i++;
					$conditions .= "$key = '$value'";
					if($i != $count) $conditions .= ' AND ';
				}
			}
			return $this->query('SELECT * FROM `'.$table.'` '.$conditions.' ORDER BY id ASC;');
		}
		
		public function find($table,$condition_array = null){
			if ($condition_array != null){
				$i = 0;
				$count = count($condition_array);
				$conditions = "WHERE ";
				foreach ($condition_array as $key => $value){
					$i++;
					$conditions .= "$key = '$value'";
					if($i != $count) $conditions .= ' AND ';
				}
			}
			return $this->query('SELECT * FROM `'.$table.'` '.$conditions.' LIMIT 1');
		}
		
		public function findById($table,$id){
			return $this->find($table,array('id' => $id));
		}
		
		public function insert($table,$fields = array()){
			$i = 0;
			$count = count($fields);
			$keys = '(';
			$values = '(';
			foreach($fields as $key => $value){
				$i++;
				$keys .= "`$key`";
				switch(strtoupper($value)){
					case 'NOW()' : case 'NOW' : $values .= 'NOW()';  break;
					default : $values .= "'$value'"; break;
				}
				if($i != $count){
					$keys .= ',';
					$values .= ',';
				}
			}
			$keys .= ')';
			$values .= ')';
			return $this->query('INSERT INTO `'.$table.'` '.$keys.' VALUES '.$values.';');
			}
		
		public function update($table,$id,$condition_array){
			$i = 0;
			$count = count($condition_array);
			$conditions = 'SET ';
			foreach($condition_array as $key => $values){
				$i++;
				$conditions .= "$key = '$values'";
				if ($i != $count) $conditions .= ', ';
			}
			return $this->query('UPDATE `'.$table.'` '.$conditions.' WHERE id = '.$id.' ;');
		}
		
		public function delete($table,$id){
			return $this->query('DELETE FROM '.$table.' WHERE id = '.$id.';');
		}
	}
?>